package com.dhcc.DRGsHosp.web.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.dayatang.utils.Page;
import org.openkoala.koala.commons.InvokeResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dhcc.DRGsHosp.facade.CliSpecFacade;
import com.dhcc.DRGsHosp.facade.DepartmentFacade;
import com.dhcc.DRGsHosp.facade.dto.DepartmentDTO;
import com.dhcc.DRGsHosp.facade.dto.IndexCalDTO;
/**
 * 
 * @ClassName: DepartmentController  
 * @Description ：    科室信息管理
 * @Date: 2017年6月28日 下午8:27:22  
 * @Author LiuHongJun 
 * @Version 2.5.0 DHC-DRGs
 */
@Controller("drgsHospWebController5")
@RequestMapping(value = "/Department")
public class DepartmentController {
		
	@Autowired
	private DepartmentFacade departmentFacade;
	
	@Autowired
	private  CliSpecFacade cliSpecFacade;
	
	@ResponseBody
	@RequestMapping("/add")
	public InvokeResult add(DepartmentDTO departmentDTO) {
		return departmentFacade.creatDepartment(departmentDTO);
	}
	
	@ResponseBody
	@RequestMapping("/update")
	public InvokeResult update(DepartmentDTO departmentDTO) {
		//更新基本信息表中的院区代码
		departmentFacade.updateBaseInfo(departmentDTO.getDepCode(),departmentDTO.getParHospCode());
		return departmentFacade.updateDepartment(departmentDTO);
	}
	
	@ResponseBody
	@RequestMapping("/pageJson")
	public Page pageJson(DepartmentDTO departmentDTO, @RequestParam int page, @RequestParam int pagesize) {
		Page<DepartmentDTO> all = departmentFacade.pageQueryDepartment(departmentDTO, page, pagesize);
		return all;
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public InvokeResult remove(@RequestParam String depCodes) {
		String[] value = depCodes.split(",");
        String[] idArrs = new String[value.length];
        for (int i = 0; i < value.length; i ++) {
        					idArrs[i] = value[i];
			        				        }
        return departmentFacade.removeDepartments(idArrs);
	}
	
	@ResponseBody
	@RequestMapping("/findDoctorInDep/{depCode}")
	public InvokeResult findDoctorInDep(@PathVariable String depCode) {
		return departmentFacade.findDoctorInDep(depCode);
	}
		
    @InitBinder    
    public void initBinder(WebDataBinder binder) {  
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");    
        dateFormat.setLenient(false);    
        //CustomDateEditor 可以换成自己定义的编辑器。
        //注册一个Date 类型的绑定器 。
        binder.setAutoGrowCollectionLimit(Integer.MAX_VALUE);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));

	}
    
    @ResponseBody
  	@RequestMapping("/findAllDep")
      public InvokeResult findAllDep(DepartmentDTO dto){
      	return InvokeResult.success(departmentFacade.findAllDep(dto));
      }

    /**
     * 
     * @methodname    :isOrderExist
     * @description   : 查询该order序号是否已存在
     * @param         :
     * @author        :fulianxia
     * @return        :
     * @date          :2015年7月1日
     */
    @ResponseBody
   	@RequestMapping("/isOrderExist")
       public Boolean isOrderExist(String order,String code){
       	return departmentFacade.isOrderExist(order,code);
       }
    
    /**
     * 
     * @methodname    :getNewAddDepNames
     * @description   : 获取新增科室名称
     * @param         :
     * @author        :mazhaoyan
     * @return        :
     * @date          :2016年5月18日
     */
    @ResponseBody
    @RequestMapping(value = "/getNewAddDepNames", method = RequestMethod.GET)
    public InvokeResult getNewAddDepNames() {
        return InvokeResult.success(departmentFacade.getNewAddDepNames());
    }
    

	@ResponseBody
	@RequestMapping(value = "/initeSpecTree")
	public InvokeResult initeSpecTree(String depCode,String verCode){
		return  InvokeResult.success(cliSpecFacade.getSpecTree(depCode,verCode)) ;
	}
	 
	@ResponseBody
	@RequestMapping(value = "/saveSelCliSpec")
	public InvokeResult saveSelCliSpec(String depCode ,String verCode , String[] cliSpecArr ){
		if(departmentFacade.saveSelCliSpec(depCode,verCode,cliSpecArr)) {
			return InvokeResult.success() ;
		} else {
			String message = "save failure";
			return InvokeResult.failure(message) ;
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/findSpecBydepCode/{depCode}")
	public InvokeResult findSpecBydepCode(@PathVariable String depCode){
		return  InvokeResult.success(departmentFacade.findSpecBydepCode(depCode)) ;
	}
	
	@ResponseBody
	@RequestMapping(value = "/findDepDRGs")
	public InvokeResult findDepDRGs(String depCode){
		return departmentFacade.findDepDRGs(depCode);
	}
	
	@ResponseBody
	@RequestMapping(value = "/findAllDepartment")
	public InvokeResult findAllDepartment(){
		return InvokeResult.success(departmentFacade.findAllDepartment());
	}
	
	/**
	 * 	查询科室组，科室，病区的编码和描述
	 *  findAllDepRelatedInfo.  
	 *  @Description：
	 *  @Author ChenHanlin
	 *  @return 
	 *  @Date:2017年12月14日上午10:36:26
	 */
	@ResponseBody
	@RequestMapping(value = "/findAllDepRelatedInfo")
	public InvokeResult findAllDepRelatedInfo(){
		return InvokeResult.success(departmentFacade.findAllDepRelatedInfo());
	}
	
	/**
	 * 
	 *  findAllHosp.  
	 *  @Description：
	 *  @Author WENYI
	 *  @return 
	 *  @Date:2018年1月26日下午4:33:24
	 */
	@ResponseBody
	@RequestMapping(value = "/findAllHosp")
	public InvokeResult findAllHosp(){
		return InvokeResult.success(departmentFacade.findAllHosp());
	}
	/**
	 * 
	 * @Title: synchroDepAndHosp  
	 * @Description:科室设置中一键同步
	 * @param @return   
	 * @return InvokeResult  
	 * @author shaozhiyuan
	 * @date 2018年3月13日  
	 * @version V3.1.0 
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value="/synchro")
	public InvokeResult synchroDepAndHosp(@RequestParam String depCodes){
		String[] value = depCodes.split(",");
        String[] idArrs = new String[value.length];
        for (int i = 0; i < value.length; i ++) {
        					idArrs[i] = value[i];
			        				        }
		return InvokeResult.success(departmentFacade.synchroDepAndHosp(idArrs));
	}
	/**
	 * 
	 *  getDepNotification.  
	 *  @Description：查询没有配置科室组的科室
	 *  @Author WENYI
	 *  @return 
	 *  @Date:2018年3月13日下午6:32:57
	 */
	@ResponseBody
	@RequestMapping(value="/getDepNotification")
	public InvokeResult getDepNotification(){
		return InvokeResult.success(departmentFacade.getDepNotification());
	}
	/**
	 * 
	 *  getDepNotification.  
	 *  @Description：查询没有配置院区的科室
	 *  @Author WENYI
	 *  @return 
	 *  @Date:2018年3月13日下午6:32:57
	 */
	@ResponseBody
	@RequestMapping(value="/getHospNotification")
	public InvokeResult getHospNotification(){
		return InvokeResult.success(departmentFacade.getHospNotification());
	}
	
	/**
	 *  查询科室应诊疗病组
	 *  findDepartmentDRGs.  
	 *  @Description：
	 *  @Author ChenHanlin
	 *  @return 
	 *  @Date:2018年3月23日上午11:32:36
	 */
	@ResponseBody
	@RequestMapping(value="/findDepartmentDRGs")
	public InvokeResult findDepartmentDRGs(IndexCalDTO dto){
		return InvokeResult.success(departmentFacade.findDepartmentDRGs(dto));
	}
}
